﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace LocalOverride.Fiddler.UrlMapping
{
    public class MappingRegistry : IMappingRegistry
    {
        readonly List<IMapping> mappings = new List<IMapping>();
        
        public MappingResult GetMapping(Uri requestUri)
        {
            var firstMapping = (from mapping in mappings
                    where mapping.Matches(requestUri) 
                    select mapping.Map(requestUri))
                    .FirstOrDefault();

            if (firstMapping == null)
                firstMapping = MappingResult.NoMapping;

            return firstMapping;
        }
       
        public void AddMapping(IMapping mapping)
        {
            mappings.Add(mapping);
        }
    }
}